﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

namespace PizzaWic.admin
{
    //permet de d'afficher et d'editer les raw_material
    public partial class RawMaterialManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //premier acces pour obtenir tout les raw material
                SqlAccess sql = new SqlAccess();
                DataSet dataSet = new DataSet();
                dataSet = sql.getAllRawMaterial();

                MonRepeater.DataSource = dataSet;
                MonRepeater.DataBind();
            }
        }

        //action sur le repeater
        protected void MonRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            SqlAccess sql = new SqlAccess();
            //edition d'une matière première
            if (e.CommandName == "edit_raw_material")
            {
                Object[] data = new Object[6];
                data[0] = e.CommandArgument;
                //mise à jour des donnée en base
                data[1] = ((TextBox)e.Item.FindControl("editName")).Text;
                data[2] = ((TextBox)e.Item.FindControl("editQuantity")).Text;
                data[3] = ((TextBox)e.Item.FindControl("editUnitaryQuantity")).Text;
                data[4] = ((TextBox)e.Item.FindControl("editAlertQuantity")).Text;
                data[5] = ((TextBox)e.Item.FindControl("editPrice")).Text; ;
                sql.setRawMaterial(data);
            }

            //suppression d'un raw_material
            else if (e.CommandName == "delete_raw_material")
            {
                Object[] param = new Object[1];
                param[0] = e.CommandArgument;
                sql.delRawMaterial(param);
            }

            //mise à jour de la vue
            DataSet dataSet = new DataSet();
            dataSet = sql.getAllRawMaterial();
            MonRepeater.DataSource = dataSet;
            MonRepeater.DataBind();
        }

        //ajout des quantité à la volée
        protected void addQuantityCmd_Click(object sender, EventArgs e)
        {
            SqlAccess sql = new SqlAccess();

            for (int count = 0; count < MonRepeater.Items.Count; count++)
            {
                HiddenField hf = (HiddenField)MonRepeater.Items[count].FindControl("HiddenFieldrm");
                TextBox txtBox = (TextBox)MonRepeater.Items[count].FindControl("addQuantity");

                //si la quantité à rajouter n'est pas nul, on ajoute en base
                if (int.Parse(txtBox.Text) != 0)
                {
                    Object[] data = new Object[2];
                    data[0] = (int.Parse(hf.Value));
                    data[1] = int.Parse(txtBox.Text) + int.Parse(((Label)MonRepeater.Items[count].FindControl("quantity")).Text);
                    sql.addRawMaterialQuantity(data);
                }
            }

            //mise à jour de la vue
            DataSet dataSet = new DataSet();
            dataSet = sql.getAllRawMaterial();

            MonRepeater.DataSource = dataSet;
            MonRepeater.DataBind();
        }
    }
}